package com.c.ctgproject.ui.collect

import android.content.Intent
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.adapter.guang.CarInPartCountAdapter
import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.databinding.ActivityCarInWarehouseBinding

import com.c.ctgproject.entity.guang.*
import com.c.ctgproject.enum.DictionaryMenuEnum
import com.c.ctgproject.event.PositionEvent
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.c.ctgproject.http.MyObserverJustOk
import com.c.ctgproject.utils.*
import com.c.ctgproject.wiget.dialog.MyBottomDialog
import com.lwkandroid.imagepicker.ImagePicker
import com.lwkandroid.imagepicker.data.ImageBean
import com.lwkandroid.widget.ninegridview.NineGridBean
import com.nanchen.compresshelper.CompressHelper
import kotlinx.android.synthetic.main.dialog_group.*
import org.greenrobot.eventbus.EventBus
import java.io.File

/**
 *作者:LiBW
 *创建日期: 2021/9/9
 *描述:车辆入库 页面 登记入库 页面(车辆)
 */
class CarInWarehouseActivity : BaseActivityV2() {
    private val mBinding by lazy { ActivityCarInWarehouseBinding.inflate(layoutInflater) }
    override fun getLayout() = mBinding.root

    //    private val oldDataStr by lazy { intent.getStringExtra(MyConstants.DATA).handlerNull() }
    private val mCarId by lazy { intent.getIntExtra(MyConstants.CAR_ID, -1) }
    private val imageShowList = ArrayList<String>()
    private val imageResultCode = 100
    private val isBohui by lazy { intent.getBooleanExtra(MyConstants.IS_BOHUI, false) }
    private var partsId: Int? = null
    private var canChange = true

    //如果type为4表示五类车 没有配件
    private var carType: Int? = null

    //如果是驳回的数据就先不获取配件信息了 等用户选择了checkbox再去获取
    private var getPartFlag = true

    //我的 编辑按钮
    private val isUpdate by lazy { intent.getBooleanExtra(MyConstants.UPDATE,false) }
    private val mListPosition by lazy { intent.getIntExtra(MyConstants.POSITION,-1) }


    //有无电池，1-有，0-无
    private var hasBattery :Int?=null

    //能源类型 1-燃油车 2-新能源车

    //1-燃油车 2-电动车 3-油电混合  车辆的登记入库
    private var energy :Int?=null

    //车辆状态 1-整车 2-车架
    private var carStatus :Int?= 1
    override fun event() {
        //handlerVinStart()
        mBinding.carVinET.handlerToUpperCase()
        mBinding.carPlateET.handlerToUpperCase()

        //Vin码查询车辆信息
        mBinding.carVinET.handlerText {
            if (canChange && it.length == 17) {
                ApiMethods.apiSubscribe(ApiMethods.retrofit.useVinGetCarModel(it), MyObserverJustOk(this) {
                    MyLog.d("回调:$it")
                    val data = getGson().fromJson(it, UseVinGetCarModelEntity::class.java).data
                    data?.description?.let { bean ->
                        mBinding.carModelET.textSet(bean)
                    }
                })
            }
        }



        mBinding.mPhotoNGV.init(this, imageShowList, 6, imageResultCode)
        ApiMethods.apiSubscribe(ApiMethods.retrofit.carDetailHasImage(mCarId), MyObserverHandlerLoading(this, loading) {
            MyLog.d("车辆详情回调:$it")
            val entity = getGson().fromJson(it, CarInfoEntityV2::class.java).data
            entity?.let { data ->
                carType = data.carType
                //1:燃油 2:纯电 3:混动 4.五类
                when (carType) {
                    1 -> {

                    }
                    2 -> {

                    }
                    4 -> {
                        //五类车没有配件
//                        mBinding.mPartLL.visibility = View.GONE
                    }
                }

                //我的重新编辑
                if (isUpdate) {
                    partList.clear()
                    data.carPartsList?.forEach { bean ->
                        val b = DictionaryChildBean()
                        b.count = bean.partsCount.handlerNull()
                        b.key = bean.partsName.handlerNull()
                        partList.add(b)
                    }
                    val adapter = CarInPartCountAdapter(this, partList)
                    mBinding.mPartCountRV.adapter = adapter
                    mBinding.mPartCountRV.layoutManager = LinearLayoutManager(this)
                }


                //驳回的单子
                if (data.entrance == 2) {
                    //驳回的单子直接将旧数据放上去就先不获取了
                    getPartFlag = false
                    partList.clear()
                    data.carPartsList?.forEach { bean ->
                        val b = DictionaryChildBean()
                        b.count = bean.partsCount.handlerNull()
                        b.key = bean.partsName.handlerNull()
                        partList.add(b)
                    }
                    val adapter = CarInPartCountAdapter(this, partList)
                    mBinding.mCarEngineET.setText(data.carEngine)
                    mBinding.mPartCountRV.adapter = adapter
                    mBinding.mPartCountRV.layoutManager = LinearLayoutManager(this)
                    mBinding.mBackLL.visibility = View.VISIBLE
                } else {
                    mBinding.mBackLL.visibility = View.GONE
                }



                partsId = data.partsId
                mBinding.carSourceET.text = data.carSource.handlerNull()
                carFrom = data.carSource.handlerNull()
                canChange = false
                mBinding.carVinET.textSet(data.carVin.handlerNull())
                canChange = true
                carStatus=data.carStatus
                when (data.carStatus) {
                    1 -> {
                        mBinding.statusSelectET.text="整车"
                        //mBinding.mZC.performClick()
                    }
                    2 -> {
                        mBinding.statusSelectET.text="车架"
                        //mBinding.mCJ.performClick()

                    }
                }
                handlerVinStart()
                mBinding.carPlateET.textSet(data.carPlate.handlerNull())
                mBinding.carModelET.textSet(data.carModel.handlerNull())
                //能源类型 1-燃油车 2-电动车 3-油电混合
                if (carType != 4) {
                    //编辑时不获取
                    if(isUpdate){
                        getPartFlag=false
                    }
                    //不是五类车并且非驳回的单子进去默认选一个
                    when (data.energy) {
                        1 -> {
                            //mBinding.mRYC.performClick()
                            showEnergy(0)
                        }
                        2 -> {
                            //mBinding.mDDC.performClick()
                            showEnergy(1)
                        }
                        3 -> {
                            //mBinding.mYDHH.performClick()
                            showEnergy(2)
                        }
                        else -> {
                            //showEnergy(0)
                            //mBinding.mRYC.performClick()
                        }
                    }

                }
                hasBattery=data.hasBattery
                when (data.hasBattery) {
                    1 -> mBinding.mBatteryET.setText("有")
                    0 -> {
                        mBinding.mBatteryET.setText("无")
                    }
                }
                val carEngineShow = when (data.carType) {
                    1 -> View.VISIBLE
                    2 -> View.VISIBLE
                    else -> View.GONE
                }
                val carType = when (data.carType) {
                    1 -> "社会车"
                    2 -> "试验车"
                    3 -> "外包车"
                    4 -> "五类车"
                    5 -> "农机具"
                    else -> "暂无数据"
                }
                mBinding.mCarEngineLL.visibility = carEngineShow
                mBinding.carType.text = carType
//                mBinding.ternaryCatalysisET.textSet(data.ternaryCatalysis.handlerNull())
//                mBinding.batteryET.textSet(data.battery.handlerNull())
//                mBinding.powerBatteryET.textSet(data.powerBattery.handlerNull())
//                mBinding.starterET.textSet(data.starter.handlerNull())
//                mBinding.alternatorET.textSet(data.alternator.handlerNull())
//                mBinding.waterTankET.textSet(data.waterTank.handlerNull())
//                mBinding.airConditionerET.textSet(data.airConditioner.handlerNull())
//                mBinding.couplingMotorET.textSet(data.couplingMotor.handlerNull())
                mBinding.logisticsNoET.textSet(data.logisticsNo.handlerNull())
                mBinding.driverName.textSet(data.driverName.handlerNull())
                mBinding.logisticsCompanyET.textSet(data.logisticsCompany.handlerNull())
                mBinding.logisticsPlateET.textSet(data.logisticsPlate.handlerNull())
                mBinding.logisticsCostET.textSet(data.logisticsCost.handlerNull())
                mBinding.sendAddressET.textSet(data.sendAddress.handlerNull())
                mBinding.receiveAddressET.textSet(data.receiveAddress.handlerNull())
//            mBinding.carNumET.textSet(data.carNum.handlerNull())
//                mBinding.chassisET.textSet(data.chassis.handlerNull())
//                mBinding.engineET.textSet(data.engine.handlerNull())
//                mBinding.transmissionCaseET.textSet(data.transmissionCase.handlerNull())
//                mBinding.hubET.textSet(data.hub.handlerNull())
//                mBinding.tyreET.textSet(data.tyre.handlerNull())
                //旧图片
                data.entrancePictures?.let { images ->
                    val resultList: MutableList<NineGridBean> = ArrayList()
                    images.forEach { bean ->
                        val nineGirdData = NineGridBean(bean)
                        imageShowList.add(bean)
                        resultList.add(nineGirdData)
                    }
                    mBinding.mPhotoNGV.addDataList(resultList)
                }
                val rejectName = data.rejectName.handlerNull()
                //驳回原因 新增判断entrance==2的时候再展示 之前是有就展示
                val remark = data.rejectReason.handlerNull()
//                if (remark.isNotEmpty()) {
//                    mBinding.mBackLL.visibility = View.VISIBLE
                mBinding.mBackTV.text = remark
                mBinding.mRejectNameTV.text = rejectName
//                }

//                mBinding.carSourceET.text = data.carSource.noDataHandler()
//                mBinding.carVinET.text = data.carVin.noDataHandler()
//                when (data.carStatus) {
//                    1 -> mBinding.mCarStatueTV.text = "整车"
//                    2 -> mBinding.mCarStatueTV.text = "车架"
//                    else -> mBinding.mCarStatueTV.text = "暂无数据"
//                }
//                mBinding.carPlateET.text = data.carPlate.noDataHandler()
//                mBinding.carModelET.text = data.carModel.noDataHandler()
//                when (data.energy) {
//                    1 -> mBinding.mBatteryModelTV.text = "燃油车"
//                    2 -> mBinding.mBatteryModelTV.text = "新能源车"
//                    else -> mBinding.mBatteryModelTV.text = "暂无数据"
//                }
//                when (data.hasBattery) {
//                    1 -> mBinding.mYDC.performClick()
//                    0 -> mBinding.mWDC.performClick()
//                }
//
//                mBinding.ternaryCatalysisET.text = data.ternaryCatalysis.noDataHandler()
//                mBinding.batteryET.text = data.battery.noDataHandler()
//                mBinding.powerBatteryET.text = data.powerBattery.noDataHandler()
//                mBinding.starterET.text = data.starter.noDataHandler()
//                mBinding.alternatorET.text = data.alternator.noDataHandler()
//                mBinding.waterTankET.text = data.waterTank.noDataHandler()
//                mBinding.airConditionerET.text = data.airConditioner.noDataHandler()
//                mBinding.couplingMotorET.text = data.couplingMotor.noDataHandler()
//                mBinding.chassisET.text = data.chassis.noDataHandler()
//                mBinding.engineET.text = data.engine.noDataHandler()
//                mBinding.transmissionCaseET.text = data.transmissionCase.noDataHandler()
//                mBinding.hubET.text = data.hub.noDataHandler()
//                mBinding.tyreET.text = data.tyre.noDataHandler()
//                //登记入库图片
//                mBinding.mPhotoList.adapter = ImageCommonRvV2Adapter(this, data.entrancePictures)
//                mBinding.mPhotoList.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
            }
        })


    }


    private fun showEnergy(type:Int){
        when(type){
            0->{
                mBinding.energySelectET.text = "燃油车"
                mBinding.mBatteryLL.visibility = View.GONE
                mBinding.mBatteryLine.visibility = View.GONE
//            mBinding.mNoEngineLL.visibility = View.VISIBLE
                energy = 1
                if (carType != 4 && getPartFlag) {
                    getPartList()
                }
                getPartFlag = true
            }
            1->{
                mBinding.energySelectET.text = "电动车"
                mBinding.mBatteryLL.visibility = View.VISIBLE
                mBinding.mBatteryLine.visibility = View.VISIBLE
//            mBinding.mNoEngineLL.visibility = View.GONE
//            mBinding.mNoPowerBatteryLL.visibility=View.VISIBLE
                energy = 2
                if (carType != 4 && getPartFlag) {
                    getPartList()
                }
                getPartFlag = true
            }
            2->{
                mBinding.energySelectET.text = "油电混合"
                mBinding.mBatteryLL.visibility = View.VISIBLE
                mBinding.mBatteryLine.visibility = View.VISIBLE
//            mBinding.mNoEngineLL.visibility = View.VISIBLE
//            mBinding.mNoPowerBatteryLL.visibility=View.VISIBLE
                energy = 3
                if (carType != 4 && getPartFlag) {
                    getPartList()
                }
                getPartFlag = true
            }
        }
    }

    //车辆来源
    private var carFrom = ""
    override fun click() {
        mBinding.mStatusSelectLL.setOnClickListener {
            val list = mutableListOf("整车", "车架")
            val dialog = MyBottomDialog.Builder(this).setOnOptionSelectedListener { position ->
                mBinding.statusSelectET.text = list[position]
                carStatus = position + 1
                handlerVinStart()
            }.setOptions(list).create()
            dialog.show()
        }
        mBinding.mEnergySelectLL.setOnClickListener {
            val list = mutableListOf("燃油车", "电动车", "油电混合")
            val dialog = MyBottomDialog.Builder(this).setOnOptionSelectedListener { position ->

              showEnergy(position)
            }.setOptions(list).create()
            dialog.show()
        }
        mBinding.mBatteryLL.setOnClickListener {
            val list = mutableListOf("无", "有")
            val dialog = MyBottomDialog.Builder(this).setOnOptionSelectedListener { position ->
                hasBattery = position
                mBinding.mBatteryET.text = list[position]
            }.setOptions(list).create()
            dialog.show()
        }
        //燃油车没有动力电池和耦合电机 进来默认是燃油车
//        mBinding.mNoPowerBatteryLL.visibility=View.GONE
        //选择来源 (供应商)
        mBinding.mDictionarySelectLL.setOnClickListener {
            DictionaryMenuActivity.jumpToDictionary(this, DictionaryMenuEnum.SUPPLIER.menuCode, true, arrayListOf(carFrom))
        }

        //燃油车
//        mBinding.mRYC.setOnClickListener {
//            mBinding.mBatteryLL.visibility = View.GONE
//            mBinding.mBatteryLine.visibility = View.GONE
////            mBinding.mNoEngineLL.visibility = View.VISIBLE
//            energy = 1
//            if (carType != 4 && getPartFlag) {
//                getPartList()
//            }
//            getPartFlag = true
////            //燃油车没有动力电池和耦合电机
////            mBinding.mNoPowerBatteryLL.visibility=View.GONE
//        }
        //电动车
//        mBinding.mDDC.setOnClickListener {
//            mBinding.mBatteryLL.visibility = View.VISIBLE
//            mBinding.mBatteryLine.visibility = View.VISIBLE
////            mBinding.mNoEngineLL.visibility = View.GONE
////            mBinding.mNoPowerBatteryLL.visibility=View.VISIBLE
//            energy = 2
//            if (carType != 4 && getPartFlag) {
//                getPartList()
//            }
//            getPartFlag = true
//        }
        //混动车
//        mBinding.mYDHH.setOnClickListener {
//            mBinding.mBatteryLL.visibility = View.VISIBLE
//            mBinding.mBatteryLine.visibility = View.VISIBLE
////            mBinding.mNoEngineLL.visibility = View.VISIBLE
////            mBinding.mNoPowerBatteryLL.visibility=View.VISIBLE
//            energy = 3
//            if (carType != 4 && getPartFlag) {
//                getPartList()
//            }
//            getPartFlag = true
//        }
        //整车
//        mBinding.mZC.setOnClickListener {
//            carStatus = 1
//            handlerVinStart()
//        }
//        //车架
//        mBinding.mCJ.setOnClickListener {
//            carStatus = 2
//            handlerVinStart()
//        }

//        mBinding.mYDC.setOnClickListener {
//            hasBattery = 1
//
//        }
//        mBinding.mWDC.setOnClickListener {
//            hasBattery = 0
//        }
        //提交
        mBinding.mConfirmBTN.setOnClickListener {



            val carSourceET = mBinding.carSourceET.text.handlerNull()
            val carVinET = mBinding.carVinET.text.handlerNull()
            val carPlateET = mBinding.carPlateET.text.handlerNull()
            val status=mBinding.statusSelectET.text.handlerNull()
            val energyText=mBinding.energySelectET.text.handlerNull()
            val battery=mBinding.mBatteryET.text.handlerNull()
//            val chassisET = mBinding.chassisET.text()
//            val engineET = mBinding.engineET.text()
//            val transmissionCaseET = mBinding.transmissionCaseET.text()
//            val hubET = mBinding.hubET.text()
//            val tyreET = mBinding.tyreET.text()
//            val ternaryCatalysisET = mBinding.ternaryCatalysisET.text()
//            val batteryET = mBinding.batteryET.text()
//            val powerBatteryET = mBinding.powerBatteryET.text()
//            val starterET = mBinding.starterET.text()
//            val alternatorET = mBinding.alternatorET.text()
//            val waterTankET = mBinding.waterTankET.text()
//            val airConditionerET = mBinding.airConditionerET.text()
//            val couplingMotorET = mBinding.couplingMotorET.text()
            energy?.let {
                if(energy!=1&&battery.isEmpty()){
                    showToast("请选择有无电池")
                    mBinding.mBatteryLL.myShake()
                    return@setOnClickListener
                }
                partList.forEach {
                    if(it.count==""){
                        showToast("请填写${it.key}的数量")
                        return@setOnClickListener
                    }
                }
            }

            when {


                status.isEmpty() -> {
                    showToast("请选择车辆状态")
                    mBinding.mStatusSelectLL.myShake()
                }

                carSourceET.isEmpty() -> {
                    showToast("请输入车辆来源")
                    mBinding.mDictionarySelectLL.myShake()
                }
                needVin && carVinET.isEmpty() -> {
                    showToast("请输入VIN码")
                    mBinding.mVinLL.myShake()
                }
                energyText.isEmpty() -> {
                    showToast("请选择能源类型")
                    mBinding.mEnergySelectLL.myShake()
                }

//                needVin && carVinET.length < 17 -> {
//                    showToast("请输入完整的VIN码")
//                    mBinding.mVinLL.myShake()
//                }
                needPlant && carPlateET.isEmpty() -> {
                    showToast("请输入车牌号")
                    mBinding.carPlateET.myShake()
                }
                needPlant && carPlateET.length < 7 -> {
                    showToast("请输入完整的车牌号")
                    mBinding.carPlateET.myShake()
                }
//                carType != 4 && chassisET.isEmpty() -> showToast("请输入底盘数量")
                //电动车没有发动机和变速箱
//                carType != 4 && energy != 2 && engineET.isEmpty() -> showToast("请输入发动机数量")
//                carType != 4 && energy != 2 && transmissionCaseET.isEmpty() -> showToast("请输入变速箱数量")
//                carType != 4 && energy != 2 && ternaryCatalysisET.isEmpty() -> showToast("请输入三元催化数量")

//                carType != 4 && hubET.isEmpty() -> showToast("请输入轮毂数量")
//                carType != 4 && tyreET.isEmpty() -> showToast("请输入轮胎数量")
//                carType != 4 && batteryET.isEmpty() -> showToast("请输入蓄电池数量")
//                carType != 4 && powerBatteryET.isEmpty() -> showToast("请输入数量")
//                carType != 4 && starterET.isEmpty() -> showToast("请输入起动机数量")
//                carType != 4 && alternatorET.isEmpty() -> showToast("请输入发电机数量")
//                carType != 4 && waterTankET.isEmpty() -> showToast("请输入水箱数量")
//                carType != 4 && airConditionerET.isEmpty() -> showToast("请输入空调数量")
//                carType != 4 && couplingMotorET.isEmpty() -> showToast("请输入耦合电机数量")

                else -> {
                    if (imageShowList.isEmpty()) {
                        showToast("请上传车辆照片")
                        return@setOnClickListener
                    }
                    //所有本地文件的图片
                    val filePathList = ArrayList<String>()
                    imageShowList.forEach {
                        if (!it.startsWith("http")) {
                            filePathList.add(it)
                        }
                    }

                    if (filePathList.isNotEmpty()) {
                        val fileList = ArrayList<File>()
                        var index = 0
                        filePathList.forEach {
                            index++
                            val file = File(it)
                            //图片压缩
                            //val newFile: File = CompressHelper.getDefault(this).compressToFile(file)


                            val newFile = ImageUtil.imageWaterMark(this, file, index,"优湃能源-入场区")
                            fileList.add(newFile)
                           // fileList.add(file)
                        }
                        upDataFile(fileList) {
                            val dataList = ArrayList<String>()
                            it?.forEach { b ->
                                dataList.add(b.fileUrl.handlerNull())
                            }
                            MyLog.d("有新的文件")
                            //旧的也给他
                            imageShowList.forEach { bean ->
                                if (bean.startsWith("http")) {
                                    dataList.add(bean)
                                }
                            }

                            submitToHttp(dataList)
                        }
                    } else {
                        //没有新文件 但是有旧的url
                        if (imageShowList.isNotEmpty()) {
                            MyLog.d("没有新文件给他旧url")
                            submitToHttp(imageShowList)
                        } else {
                            MyLog.d("啥都没有")
                            submitToHttp(arrayListOf())
                        }
                    }
                }
            }

        }
    }

    //    private var needVin = true
//    private var needPlant=true
    private var needVin = false
    private var needPlant=false
    private fun handlerVinStart() {
//        if (carStatus == 2) {
//            mBinding.mVinStart.visibility = View.INVISIBLE
//            needVin = false
//        } else {
//            if (carType == 1 || carType == 3) {
//                mBinding.mPlateStar.visibility= View.VISIBLE
//                mBinding.mVinStart.visibility = View.VISIBLE
//                needVin = true
//                needPlant=true
//            } else {
//                mBinding.mPlateStar.visibility= View.INVISIBLE
//                mBinding.mVinStart.visibility = View.INVISIBLE
//                needVin = false
//                needPlant=false
//            }
//        }
        if((carType==1||carType==3)&&carStatus!=2){
            mBinding.mVinStart.visibility = View.VISIBLE
            needVin = true
        }else{
            mBinding.mVinStart.visibility = View.GONE
            needVin = false
        }
        if(carType==1){
            mBinding.mPlateStar.visibility= View.VISIBLE
            needPlant=true
        }
    }

    private fun submitToHttp(imageList: ArrayList<String>) {
        val map = HashMap<String, Any?>()
        map["carId"] = mCarId
        map["carSource"] = mBinding.carSourceET.text.handlerNull()
        map["carVin"] = mBinding.carVinET.text.handlerNull()
        map["carPlate"] = mBinding.carPlateET.text.handlerNull()
        map["carModel"] = mBinding.carModelET.text.handlerNull()
//        map["chassis"] = mBinding.chassisET.text()


        //五类车没有配件
        if (carType != 4) {
            val carPartsList = ArrayList<CarPartsListBean>()
            partList.forEach { bean ->
                carPartsList.add(CarPartsListBean(bean.key.handlerNull(), bean.count))
            }
            map["carPartsList"] = carPartsList

//            if (energy != 2) {
            //纯电动车没有这些玩意
//                map["engine"] = mBinding.engineET.text()
//                map["transmissionCase"] = mBinding.transmissionCaseET.text()
//                map["ternaryCatalysis"] = mBinding.ternaryCatalysisET.text()
//            }
//            map["hub"] = mBinding.hubET.text()
//            map["tyre"] = mBinding.tyreET.text()
//            map["battery"] = mBinding.batteryET.text()
//            map["powerBattery"] = mBinding.powerBatteryET.text()
//            map["starter"] = mBinding.starterET.text()
//            map["alternator"] = mBinding.alternatorET.text()
//            map["waterTank"] = mBinding.waterTankET.text()
//            map["airConditioner"] = mBinding.airConditionerET.text()
//            map["couplingMotor"] = mBinding.couplingMotorET.text()
        }
        map["logisticsNo"] = mBinding.logisticsNoET.text.handlerNull()
        map["logisticsCompany"] = mBinding.logisticsCompanyET.text.handlerNull()
        map["sendAddress"] = mBinding.sendAddressET.text.handlerNull()
        map["receiveAddress"] = mBinding.receiveAddressET.text.handlerNull()
        map["logisticsPlate"] = mBinding.logisticsPlateET.text.handlerNull()
        map["logisticsCost"] = mBinding.logisticsCostET.text.handlerNull()
        map["driverName"] = mBinding.driverName.text.handlerNull()
//            map["carNum"]=mBinding.carNumET.text.handlerNull()
        map["carStatus"] = carStatus
        map["energy"] = energy
        map["carEngine"]=mBinding.mCarEngineET.text.handlerNull()
        //是否是驳回的
        if (isBohui) {
            map["partsId"] = partsId
        }


        val images = ArrayList<String>()
        images.addAll(imageList)
        map["picUrls"] = images
        //新能源车 油电混合 将有无电池传给后台
        if (energy == 2||energy == 3) map["hasBattery"] = hasBattery
        if(needVin&&mBinding.carVinET.text.handlerNull().length < 17){
            commonDialog("输入的vin码小于17位，是否继续提交"){
                ApiMethods.apiSubscribe(ApiMethods.retrofit.carInWarehouse(handlerBody(map)), MyObserverHandlerLoading(this, loading) {
                    MyLog.d("提交回调:$it")
                    showToast("提交成功")
                    EventBus.getDefault().post(PositionEvent(mListPosition))
                    finish()
                })
            }
        }else{
            ApiMethods.apiSubscribe(ApiMethods.retrofit.carInWarehouse(handlerBody(map)), MyObserverHandlerLoading(this, loading) {
                MyLog.d("提交回调:$it")
                showToast("提交成功")
                EventBus.getDefault().post(PositionEvent(mListPosition))
                finish()
            })
        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        data?.let {
            when (requestCode) {
                //图片回调
                imageResultCode -> {
                    val list: List<ImageBean>? = data.getParcelableArrayListExtra(ImagePicker.INTENT_RESULT_DATA)
                    val resultList: MutableList<NineGridBean> = ArrayList()
                    list?.forEach { imageBean ->
                        val nineGirdData = NineGridBean(imageBean.imagePath)
                        imageShowList.add(imageBean.imagePath)
                        resultList.add(nineGirdData)
                    }
                    mBinding.mPhotoNGV.addDataList(resultList)
                }
                1000 -> {
                    val name = data.getStringExtra(MyConstants.NAME)
//                val abbreviation=result.getStringExtra(MyConstants.ABBREVIATION)
                    carFrom = name.handlerNull()
                    mBinding.carSourceET.text = name.handlerNull()
                }
                else -> {

                }
            }
        }
    }

    private val partList = ArrayList<DictionaryChildBean>()
    private fun getPartList() {
        //散件
        ApiMethods.apiSubscribe(ApiMethods.retrofit.getDictionaryMenuV2(energy = energy, isCheckParts = 1), MyObserverHandlerLoading(this, loading) {
            MyLog.d("散件回调:$it")
            val data = getGson().fromJson(it, DictionaryPartMenuEntity::class.java).data
            data?.let { list ->
                //重新编辑就不再获取了
//                if (isUpdate){
//                    return@MyObserverHandlerLoading
//                }
                partList.clear()
                partList.addAll(list)
                val adapter = CarInPartCountAdapter(this, partList)
                mBinding.mPartCountRV.adapter = adapter
                mBinding.mPartCountRV.layoutManager = LinearLayoutManager(this)
            }
        })
    }
}